package com.awesome.javademo.threads.thread.sort;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.javademo.R;
import com.awesome.common.util.log.ShowLogUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SortThreadActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_thread);
//        join();
//        orderExecuteThread();
        countDownLatch();
    }

    private void join() {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("thread1 start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("thread1 end");
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("thread2 start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("thread2 end");
            }
        });
        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();
        try {
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 顺序执行线程
     */
    private void orderExecuteThread(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("第一个线程开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("第一个线程结束执行");
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("第二个线程开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("第二个线程结束执行");
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("第三个线程开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("第三个线程结束执行");
            }
        });
    }

    /**
     * 侵入性强
     */
    private void countDownLatch() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("thread1 start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("thread1 end");
                countDownLatch.countDown();
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                ShowLogUtil.info("thread2 start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ShowLogUtil.info("thread2 end");
            }
        });
        thread1.start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();
    }
}